<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData,subOnLoad, changeEditor,configSelectData,hasValue} from '@/static/js/formUtil.js';
	import { errorModal,confirmModal,ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				instId:"",
				posStr:"",
				exists:[],
				pList:[],
				userId:"",
				codeName:"",
				userName:'',
				origPosList:[],
				entrydate:'',
				title:'',
				newReplaceUserId:''
			})
		},
		methods:{
			clearNewReplaceUser(){
				let _that = this
				_that.newReplaceUserId = _that.$refs.autoform.formdata.newReplaceUserId
				_that.$refs.autoform.clear("title_new","newReplaceUserId")
			},
			clearNewProbation(){
				let _that = this
				_that.$refs.autoform.clear("title_new","probationMonthNum")
				_that.$refs.autoform.clear("title_new","newProbation")
			},
			verifySubConfirm(){
				let _that = this
				if(_that.$refs.autoform.formdata.probationMonthNum || _that.$refs.autoform.formdata.newProbation){
					if(!_that.$refs.autoform.formdata.newWorkDate){ 
						let title =  _that.$refs.autoform.getEditorTitle("title_new","newWorkDate")
						_that.om.toast("请先选择" + title)
						if(_that.$refs.autoform.formdata.probationMonthNum){
							_that.$refs.autoform.clear("title_new","probationMonthNum")
						}
					}else{
						return true
					}
				}else{
					return true
				}
			},
			onSelectUser(item){
				let _that = this
				if(_that.newReplaceUserId == item.userId){
					return
				}
				_that.om.request(
					"common/verifyAdjust",
					{
						userId:item.userId
					}
				).then(res=>{
					if(res.value){
						errorModal(_that,'提示信息',res.value,()=>{
							_that.$refs.autoform.clear("title_new","newReplaceUserId")
						})
					}
				})
			},
			newWorkSelect(val){
				let _that = this
				_that.$refs.autoform.changeEditor("title_new","probationDate","type","datebox")
				_that.$refs.autoform.changeEditor("title_new","probationDate","prompt","请选择")
				if(_that.$refs.autoform.formdata.newProbation){
					_that.onInputProbation(_that.$refs.autoform.formdata.newProbation)
				}
				if(_that.$refs.autoform.formdata.newEffectWayCode == "35519"){
					if(moment(val).isAfter(moment())){
						errorModal(_that,'提示信息','请选择当前日期之前的时间',()=>{
							_that.$refs.autoform.clear("title_new","newWorkDate")
							_that.$refs.autoform.clear("title_new","probationDate")
						})
					}
				}else if(_that.$refs.autoform.formdata.newEffectWayCode == "36130"){
						if(!moment(_that.entrydate).isBefore(moment(val))){
							errorModal(_that,'提示信息','请选择'+_that.title+_that.entrydate+ '之后的时间',()=>{
								_that.$refs.autoform.clear("title_new","newWorkDate")
							})
						}
					}
			},
			getPosInfo(id){
				let _that = this
				_that.om.request(
					"teRegister/getPosInfo",
					{
						posId:id
					}
				).then(res=>{
					_that.$refs.autoform.formdata.newClassesName = res.value.classesName
					_that.$refs.autoform.formdata.newSalaryAccountingGroup = res.value.salaryAccountingGroup
				})
			},
			clearProbation(){
				this.$refs.autoform.clear("title_new","newProbation")
				this.$refs.autoform.clear("title_new","probationMonthNum")
				this.$refs.autoform.clear("title_new","probationDate")
			},
			clearProbationDate(){
				this.$refs.autoform.clear("title_new","newProbation")
				this.$refs.autoform.clear("title_new","probationMonthNum")
				this.$refs.autoform.clear("title_new","probationDate")
			},
			getParentUserNameByPosId(posId){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/getParentUserNameByPosId",
					{
						posId:posId
					}
				).then(res=>{
					_that.$refs.autoform.formdata.newParentUserName = res.value.username
				})
			},
			onInputProbation(num){
				if(num){
					let dateNum = Math.round(Number(num))
					if(this.$refs.autoform.formdata.newWorkDate){
						var d1 = this.moment(this.$refs.autoform.formdata.newWorkDate)
						var probationDate = d1.add(num, "days").format("YYYY-MM-DD")
						this.$refs.autoform.formdata.probationDate = probationDate
					}else{
						let title = this.$refs.autoform.getEditorTitle("title_new","newWorkDate")
						this.om.toast('请先选择'+title)
						return
					}
					this.$refs.autoform.formdata.newProbation = dateNum
					this.$refs.autoform.clear("title_new","probationMonthNum")
				}else{
					this.$refs.autoform.clear("title_new","probationMonthNum")
					this.$refs.autoform.clear("title_new","probationMonth")
					this.$refs.autoform.clear("title_new","probationDate")
				}
				
			},
			onInputProbationMonth(val){
				let _that = this
				if(val){
					let num = Math.round(Number(val))
						let date = ''
					let days = ''
					if(_that.$refs.autoform.formdata.newWorkDate){
						date = moment(_that.$refs.autoform.formdata.newWorkDate).add(num, 'M').format('YYYY-MM-DD');
						days = moment(date).diff(moment(_that.$refs.autoform.formdata.newWorkDate),'day')
					}else{
						let title = this.$refs.autoform.getEditorTitle("title_new","newWorkDate")
						this.om.toast('请先选择'+title)
						return
					}
					_that.$refs.autoform.formdata.probationMonthNum = num
					_that.$refs.autoform.formdata.probationDate = date
					_that.$refs.autoform.formdata.newProbation = days
					_that.om.request(
						"hrCommon/verifySaveProbation",
						{
							day: days,
							posId: _that.$refs.autoform.formdata.newPosId
						}
					).then(res=>{
						if(res.value.flag){
							confirmModal(_that,'提示信息',ignoreHtml(res.value.str),()=>{
								
							},()=>{
								_that.$refs.autoform.clear("title_new",'probationMonthNum')
								_that.$refs.autoform.clear("title_new","probationDate")
								_that.$refs.autoform.clear("title_new",'newProbation')
							})
						}
						
					})
				}
			},
			clearNewWorkDate(){
				let _that = this
				_that.$refs.autoform.clear("title_new","newWorkDate")
				_that.$refs.autoform.clear("title_new","probationDate")
				_that.$refs.autoform.clear("title_new","newProbation")
				_that.$refs.autoform.clear("title_new","probationMonthNum")
			},
			onSelectProbationDate(date){
				let _that = this
				if(!moment(_that.$refs.autoform.formdata.newWorkDate).isBefore(date) || date == _that.$refs.autoform.formdata.newWorkDate){
					_that.$refs.autoform.clear("title_new","probationDate")
					_that.$refs.autoform.clear("title_new","newProbation")
					let title = _that.$refs.autoform.getEditorTitle("title_new","newWorkDate")
					_that.om.toast("请选择"+title+"之后的时间")
					return 
				}
				let day = Math.abs(moment(_that.$refs.autoform.formdata.newWorkDate).diff(date,'day'));
				_that.$refs.autoform.formdata.newProbation =  day
				_that.om.request(
					"hrCommon/verifySaveProbation",
					{
						day: day,
						posId: _that.$refs.autoform.formdata.newPosId
					}
				).then(res=>{
					if(res.value.str){
						confirmModal(_that,"提示信息",ignoreHtml(res.value.str),()=>{},()=>{
							_that.$refs.autoform.clear("title_new","probationDate")
							_that.$refs.autoform.clear("title_new","newProbation")
						})
					}
				})
			},
			onSelectNewWorkDate(date){
				let _that = this
				let date1 = moment().format("YYYY-MM-DD")
				_that.om.request(
					"tempInstAdjust/getProbation",
					{
						userId:_that.userId,
						workDate:date
					}
				).then(res=>{
					_that.$refs.autoform.clear("title_new","probationDate")
					_that.$refs.autoform.clear("title_new","newProbation")
					if(res.value != 0){
						_that.$refs.autoform.formdata.newProbation = res.value
					}else{
						_that.$refs.autoform.changeEditor("title_new","probationDate","type","datebox")
						_that.$refs.autoform.changeEditor("title_new","probationDate","prompt","请选择")
					}
				})
			},
			onSelectNewEffectWay(item,entry){
				let _that = this
				let now = moment().format("YYYY-MM-DD")
				if(item.code == '36130'){
					_that.$refs.autoform.changeEditor("title_new","newWorkDate","required",true)
				}else{
					_that.$refs.autoform.changeEditor("title_new","newWorkDate","required",false)
				}
				_that.$refs.autoform.changeEditor("title_new","newWorkDate","type","datebox")
				_that.$refs.autoform.changeEditor("title_new","newWorkDate","prompt","请选择")
				_that.$refs.autoform.changeEditor("title_new","newWorkDate","onSelect","newWorkSelect")
				if(!entry){
					_that.$refs.autoform.clear("title_new","newWorkDate")
					_that.$refs.autoform.clear("title_new","probationDate")
				}
			},
			getNewPosWay1(){
				let _that = this
				_that.om.request(
					"tempInstAdjust/getNewPosWay1"
				).then(res=>{
					_that.$refs.autoform.changeEditor("title_new","newEffectWayCode","type","selectbox")
					_that.$refs.autoform.changeEditor("title_new","newEffectWayCode","data",configSelectData(res,'code','dicname'))
					_that.$refs.autoform.changeEditor("title_new","newEffectWayCode","onSelect","onSelectNewEffectWay")
					_that.$refs.autoform.initSelectIndex("title_new","newEffectWayCode")
				})
			},
			getNewReplaceUser(id,entry){
				let _that = this
				_that.om.request(
					"teRegister/getEmpByPosId",
					{
						posId:id
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title_new","newReplaceUserId","type","selectbox")
					_that.$refs.autoform.changeEditor("title_new","newReplaceUserId","data",configSelectData(res,"userId","userName"))
					_that.$refs.autoform.changeEditor("title_new","newReplaceUserId","required",true)
					_that.$refs.autoform.changeEditor("title_new","newReplaceUserId","onSelect",'onSelectUser')
					if(entry){
						_that.$refs.autoform.initSelectIndex("title_new","newReplaceUserId")
					}else{
						if(res.length==1){
							_that.$refs.autoform.initSelectVal("title_new","newReplaceUserId")
						}
					}
				})
			},
			onSelectNewPos(item,entry){
				let _that = this
				_that.om.request(
					"tempInstAdjust/getWorkTypeList",
					{
						posIds1:_that.pList,
						userId: _that.userId,
						posId: item.posid,
						typeCode: _that.$refs.autoform.formdata.newReasonCode
					}
				).then(res=>{
					if(!entry){
						_that.$refs.autoform.clear("title_new","newReplaceUserId")
					}
					_that.$refs.autoform.changeEditor("title_new","newProbation","type","numberbox")
					_that.$refs.autoform.changeEditor("title_new","newProbation","prompt","请输入")
					_that.$refs.autoform.changeEditor("title_new","newWorkTypeCode","data",configSelectData(res,'code',"dicname"))
					_that.$refs.autoform.changeEditor("title_new","newWorkTypeCode","type","selectbox")
					_that.$refs.autoform.initSelectIndex("title_new","newWorkTypeCode")
					_that.$refs.autoform.changeEditor("title_new","probationMonthNum","type","numberbox")
					_that.$refs.autoform.changeEditor("title_new","probationMonthNum","prompt","请输入")
					if(_that.$refs.autoform.formdata.newReasonCode == "35506"){
						_that.getNewReplaceUser(item.posid,entry)
					}
					_that.getNewPosWay1()
					_that.$refs.autoform.changeEditor("title_new","newSalaryPos","type","switchbox")
					_that.getParentUserNameByPosId(item.posid)
					_that.getPosInfo(item.posid)
				})
			},
			changeNewPosType(item){
				let _that = this
				_that.om.request(
					"hrPosUtil/verifyReasonCode",
					{
						lList:_that.exists,
						posStr:_that.posStr,
						reasonCode:item.code,
						busCode:_that.busCode || 'HRS0125'
					},
					"post"
				).then(res=>{
					_that.$refs.autoform.changeEditor("title_new","newPosId","type","selectbox")
					_that.$refs.autoform.changeEditor("title_new","newPosId","data",configSelectData(res.value.attributes.pos,"posid","posname"))
					_that.$refs.autoform.initSelectIndex("title_new","newPosId")
				})
			},
			onSelectNewReason(item){
				let _that = this
				if(item.code == "23753"){
					if(JSON.parse(_that.origPosList)){
						for(let i of JSON.parse(_that.origPosList)){
							if(i.origHandleWayCode != "35516"){
								errorModal(_that,"提示信息",item.dicname+"必须是当前无任职岗位人员;"+_that.userName+"当前有任职岗位，如做"+item.dicname+"需将原岗位处理方式选择"+_that.codeName+"方可办理！",()=>{
									_that.$refs.autoform.clearGroup("title_new","objStr2")
									for(let item of _that.$refs.autoform.configData[0].editors){
										if(item.valueField != 'newReasonCode'){
											_that.$refs.autoform.changeEditor("title_new",item.valueField,"type","labelbox")
										}
									}
								})
								return
							}
						}
					}
				}
					_that.om.request(
						"hrPosUtil/verifyReasonCode",
						{
							lList:_that.exists,
							posStr:_that.posStr,
							reasonCode:item.code,
							busCode:_that.busCode || 'HRS0125'
						},
						"post"
					).then(res=>{
						if(res.value.checkbox && res.value.field){
							errorModal(_that,'提示信息',ignoreHtml(res.value.field),()=>{
								_that.$refs.autoform.clear("title_new","newReason")
							})
						}else{
							_that.$refs.autoform.clear("title_new","newPosId")
							_that.$refs.autoform.clear("title_new","newWorkTypeCode")
							_that.$refs.autoform.clear("title_new","newReplaceUserId")
							_that.$refs.autoform.clear("title_new","newSalaryPos")
							_that.$refs.autoform.clear("title_new","newParentUserName")
							_that.$refs.autoform.clear("title_new","newEffectWayCode")
							_that.$refs.autoform.clear("title_new","newWorkDate")
							_that.$refs.autoform.clear("title_new","probationDate")
							_that.$refs.autoform.clear("title_new","newProbation")
							_that.$refs.autoform.changeEditor("title_new","newPosId","type","selectbox")
							_that.$refs.autoform.changeEditor("title_new","newPosId","data",configSelectData(res.value.attributes.pos,"posid","posname"))
							_that.$refs.autoform.initSelectIndex("title_new","newPosId")
							_that.$refs.autoform.changeEditor("title_new","newWorkTypeCode","type","labelbox")
							_that.$refs.autoform.changeEditor("title_new","newReplaceUserId","type","labelbox")
							_that.$refs.autoform.changeEditor("title_new","newReplaceUserId","required",false)
							_that.$refs.autoform.changeEditor("title_new","newSalaryPos","type","labelbox")
							_that.$refs.autoform.changeEditor("title_new","newWorkDate","type","labelbox")
						}
					})
			},
			initLoad(){
				let _that = this
				let pro1 = _that.om.request("tempInstAdjust/getReasonCodeList",{instId:_that.instId})
				Promise.all([pro1]).then(res=>{
					_that.$refs.autoform.changeEditor("title_new","newReasonCode","data",configSelectData(res[0],"code","dicname"))
					_that.$refs.autoform.initSelectIndex("title_new","newReasonCode")
				})
			}
		},
		onShow() {
			
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				_that.instId = e.instId
				_that.posStr = e.posStr
				_that.pList = e.posIdList
				_that.userId = e.userId
				_that.userName = e.userName
				_that.origPosList = e.posList
				_that.entrydate = e.entrydate
				_that.title = e.title
				_that.exists = _that.datas.filter((e,i)=>{
					return (i != _that.index)
				}).map(e=>e.newPosId).join(",")
				if(e.edit == 'false'){
					_that.edit = false
				}else{
					_that.edit = true
				}
			if(e.configDataCacheKey){
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							_that.initLoad()
								for(let item of JSON.parse(e.origHandleWayList)){
									if(item.code == "35516"){
										_that.codeName = item.dicname
									}
								}
							}
							if(_that.formdata.newEffectWayCode){
								if(_that.formdata.newEffectWayCode == '36130'){
									changeEditor("title_new","newWorkDate","required",true,_that.autoformdata)
								}else{
									changeEditor("title_new","newWorkDate","required",false,_that.autoformdata)
								}
								changeEditor("title_new","newWorkDate","type","datebox",_that.autoformdata)
								changeEditor("title_new","newWorkDate","prompt","请选择",_that.autoformdata)
								changeEditor("title_new","newWorkDate","onSelect","newWorkSelect",_that.autoformdata)
							}
							if(_that.formdata.newWorkDate){
								changeEditor("title_new","probationDate","prompt","请选择",_that.autoformdata)
								changeEditor("title_new","probationDate","type","datebox",_that.autoformdata)
							}
						}
					})
				}
				_that.$nextTick(function(){
					for(let index in _that.datas){
						if(_that.datas[index].newSalaryPos && index != _that.index){
							_that.$refs.autoform.changeEditor("title_new",'newSalaryPos',"disabled",true)
						}
					}
					if(_that.$refs.autoform.formdata.newReasonCode){
						_that.changeNewPosType({code:_that.$refs.autoform.formdata.newReasonCode})
					}
					if(_that.$refs.autoform.formdata.newPosId){
						_that.onSelectNewPos({posid:_that.$refs.autoform.formdata.newPosId},true)
					}
				})
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                         
